# Nushell Environment Config File

def create_left_prompt [] {
    mut prompt = ($env.PWD | str replace $env.HOME '~')

    let git_branch_out = (do -s { git branch } | complete)
    if $git_branch_out.exit_code == 0 {
        let result = ($git_branch_out.stdout | lines | parse "* {branch}")
        $prompt = $"($prompt) \(($result.0.branch)\)"
    }

    $prompt
}

def create_right_prompt [] {
    let time_segment = ([
        (date now | date format '%r')
    ] | str collect)

    $time_segment
}

# Use nushell functions to define your right and left prompt
let-env PROMPT_COMMAND = { create_left_prompt }
let-env PROMPT_COMMAND_RIGHT = { create_right_prompt }

# The prompt indicators are environmental variables that represent
# the state of the prompt
let-env PROMPT_INDICATOR = { "〉" }
let-env PROMPT_INDICATOR_VI_INSERT = { "〉" }
let-env PROMPT_INDICATOR_VI_NORMAL = { ": " }
let-env PROMPT_MULTILINE_INDICATOR = { "::: " }

# Specifies how environment variables are:
# - converted from a string to a value on Nushell startup (from_string)
# - converted from a value back to a string when running external commands (to_string)
# Note: The conversions happen *after* config.nu is loaded
let-env ENV_CONVERSIONS = {
  "PATH": {
    from_string: { |s| $s | split row (char esep) }
    to_string: { |v| $v | path expand | str collect (char esep) }
  }
  "Path": {
    from_string: { |s| $s | split row (char esep) }
    to_string: { |v| $v | path expand | str collect (char esep) }
  }
}

# Directories to search for scripts when calling source or use
#
# By default, <nushell-config-dir>/scripts is added
let-env NU_LIB_DIRS = [
    ($nu.config-path | path dirname | path join 'scripts')
]

# Directories to search for plugin binaries when calling register
#
# By default, <nushell-config-dir>/plugins is added
let-env NU_PLUGIN_DIRS = [
    ($nu.config-path | path dirname | path join 'plugins')
]

# To add entries to PATH (on Windows you might use Path), you can use the following pattern:
# let-env PATH = ($env.PATH | split row (char esep) | prepend '/some/path')
def-env prepend_path [dir] {
    if ($dir | path exists) {
        let-env PATH = ($env.PATH | split row (char esep) | prepend $dir | uniq)
    }
}

def-env append_path [dir] {
    if ($dir | path exists) {
        let-env PATH = ($env.PATH | split row (char esep) | append $dir | uniq)
    }
}


let-env HOMEBREW_BREW_GIT_REMOTE = 'https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git'
let-env HOMEBREW_CORE_GIT_REMOTE = 'https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git'
let-env HOMEBREW_BOTTLE_DOMAIN = 'https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles'

let-env RUSTUP_DIST_SERVER = 'https://rsproxy.cn'
let-env RUSTUP_UPDATE_ROOT = 'https://rsproxy.cn/rustup'

let-env EDITOR = 'nvim'

let fnm_exist = (not (which fnm | is-empty))
load-env (if $fnm_exist {
    fnm env --shell bash | lines |
        str replace 'export ' '' | str replace -a '"' '' |
        split column = | rename name value |
        where name != "FNM_ARCH" and name != "PATH" |
        reduce -f {} {|it, acc| $acc | upsert $it.name $it.value }
#'"'
} else {
    {}
})

if $nu.os-info.name != 'windows' {
    if $fnm_exist {
        prepend_path ($env.FNM_MULTISHELL_PATH | path join bin)
    }

    prepend_path '~/.local/bin'
    prepend_path $"($env.HOME)/.ghcup/bin"
    prepend_path $"($env.HOME)/.cabal/bin"
    append_path $"($env.HOME)/Codes/chromium/depot_tools"
    append_path '/mnt/c/Users/youz/AppData/Local/Programs/Microsoft VS Code/bin'
}
